package com.wl.dzt.basic.service;

import com.wl.dzt.basic.entity.*;
import com.wl.dzt.basic.service.callback.CreateAreaCallback;
import com.wl.dzt.sys.core.dao.DB;
import com.wl.dzt.sys.core.dao.JDao;
import com.wl.dzt.sys.util.MathUtils;
import com.wl.dzt.sys.util.StringUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/10/010
 * @description 组织机构服务层
 * 
 */
@Service
public class BasicOrganizationService {

	@Resource
	private JDao dao;

	/**
	 * <b>创建企业域</b>
	 * <p>
	 * 添加组织机构<br>
	 * 添加组织机构超级管理员<br>
	 * 添加超级管理员角色<br>
	 * 添加企业域配置项<br>
	 * 把所有数据关联再一起
	 * </p>
	 *
	 * @param organization
	 * @return
	 */
	public boolean createArea(BasicOrganization organization, CreateAreaCallback callback) {
		return DB
				.startTx(() -> {
					try {
						// 创建管理员
						BasicUser user = new BasicUser();
						int pwd = MathUtils.random.nextInt(99999999);
						user.setPhone(organization.getOrganUserPhone());
						user.setPwd(DigestUtils.md5Hex(pwd + ""));
						user.setName(organization.getOrganUserName());
						user.setUname(organization.getOrganCode());
						user.setState("4");
						user.setId(getAdminId());
						user.setAreaId(organization);
						user.setOrganId(organization);
						user.setCreateDate(new Date());
						user.setEmail("未知");
						user.setQq("未知");
						user.setIdcard("未知");
						user.setUserImg("../../area-default.png");
						DB.J_DAO.insert(user);

						// 创建管理员角色
						BasicRole basicRole = new BasicRole();
						basicRole.setRoleName(organization.getOrganName() + "管理员");
						basicRole.setOrganId(organization);
						basicRole.setRoleSort(0);
						basicRole.setRoleType("4");
						basicRole.setRoleReson("企业域管理员由系统自动创建");
						basicRole.setRoleCode("admin-area-" + System.currentTimeMillis());
						DB.save(basicRole);

						// 关联管理员
						BasicRoleUser basicRoleUser = new BasicRoleUser();
						basicRoleUser.setRoleId(basicRole);
						basicRoleUser.setUserId(user);
						DB.save(basicRoleUser);

						// 创建关联企业域配置项
						BasicConfig config = new BasicConfig();
						config.setEmailFlag("1");
						config.setImgFileSize(1D);
						config.setMsgFlag("0");
						config.setOrganId(organization);
						config.setSysName(organization.getOrganName());
						config.setDemainName("无");
						config.setMainLogo(organization.getOrganImg());
						DB.save(config);

						if (callback != null) {
							callback.excute(organization, user, pwd + "");
						}
					} catch (Exception e) {
						e.printStackTrace();
						return false;
					}
					return true;
				});
	}

	/**
	 * <b>停封企业域</b>
	 * <p>
	 * 改变企业域状态 被停封的企业域无法登陆系统
	 * </p>
	 *
	 * @param id
	 * @param state
	 * @return
	 */
	public boolean changeAreaState(String id, String state) {
		BasicOrganization area = dao.get(BasicOrganization.class, id);
		area.setState(state);
		dao.save(area);
		return true;
	}

	/**
	 * <b>获取管理员账户id</b>
	 * <p>
	 * 获取管理员账户id ,已'admin' 作为ID的开头标识
	 * </p>
	 *
	 * @return
	 */
	private static String getAdminId() {
		String id = StringUtil.uuid();
		return "admin" + id.substring(0, 27);
	}
}
